package com.example.openguass.controller;


import com.example.openguass.entity.Category;
import com.example.openguass.entity.CommonResult;
import com.example.openguass.service.CategoryService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.CategoryPageVO;
import com.example.openguass.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
@Api(value = "CategoryController", tags = "种类接口")
public class CategoryController {
    private CategoryService categoryService;
    @Autowired
    private void setCategoryService(CategoryService categoryService){
        this.categoryService = categoryService;
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody CategoryVO categoryVO){
        CommonResult result = new CommonResult().init();
        if (categoryService.exit(categoryVO.getName())){
            return (CommonResult) result.failCustom(-10086,"当前种类已存在");
        }
        if (categoryService.insert(categoryVO)){
            return result.success("创建种类成功",categoryVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"创建种类失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) CategoryVO categoryVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (categoryService.getById(categoryVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前种类");
        }
        if (categoryService.update(categoryVO)){
            return result.success("category",categoryVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新种类失败");
        }
    }

    @GetMapping("/delete/{categoryId}")
    public CommonResult delete(@PathVariable String categoryId){
        CommonResult result = new CommonResult().init();
        if (categoryService.getById(categoryId) == null){
            return (CommonResult) result.failCustom(-10086,"该种类不存在");
        }
        if (categoryService.removeAllById(categoryId)){
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除种类失败");
        }
    }

    @GetMapping("/select/{categoryId}")
    public CommonResult select(@PathVariable String categoryId){
        CommonResult result = new CommonResult().init();
        if (categoryService.getById(categoryId) == null){
            return (CommonResult) result.failCustom(-10086,"该种类不存在");
        }
        Category category = categoryService.getById(categoryId);
        return result.success("category",category);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody CategoryPageVO categoryPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        result.success("page",categoryService.findPage(categoryPageVO));
        result.end();
        return result;
    }
}
